package com.idc.quotation.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class NewTxtUtil {

	public static void readTxtFile(String filePath, int num, String encoding, Consumer<List<String>> consumer) {
		readTxtFile(filePath, num, Charset.forName(encoding), consumer);
	}

	public static void readTxtFile(String filePath, Consumer<List<String>> consumer) {
		readTxtFile(filePath, 100, StandardCharsets.UTF_8, consumer);
	}

	public static void readTxtFile(String filePath, int num, Charset charset, Consumer<List<String>> consumer) {
		File file = new File(filePath);
		if (file.isFile() && file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(file);
				readTxtFile(inputStream, num, charset, consumer);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("找不到指定的文件");
		}
	}

	public static void readTxtFile(InputStream inputStream, int num, String encoding, Consumer<List<String>> consumer) {
		readTxtFile(inputStream, num, Charset.forName(encoding), consumer);
	}

	public static void readTxtFile(InputStream inputStream, int num, Charset charset, Consumer<List<String>> consumer) {
		InputStreamReader read = null;
		BufferedReader bufferedReader = null;
		List<String> list = new ArrayList<>();
		try {
			if (null != inputStream) { // 判断文件是否存在
				read = new InputStreamReader(inputStream, charset);// 考虑到编码格式
				bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					list.add(lineTxt);
					if (list.size() >= num) {
						consumer.accept(list);
						list.clear();
					}
				}
				if (list.size() > 0) {
					consumer.accept(list);
					list.clear();
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		} finally {
			if (null != read) {
				try {
					read.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (null != inputStream) {
				try {
					inputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (null != bufferedReader) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void writeTxt(Collection<String> list, String fileName) {
		writeTxt(list, fileName, StandardCharsets.UTF_8, false);
	}

	public static void writeSimple(String fileName, String... list) {
		BufferedWriter writer = null;
		OutputStreamWriter writerStream = null;
		try {
			writerStream = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
			writer = new BufferedWriter(writerStream);
			for (String s : list) {
				writer.append(s);
				writer.newLine();
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != writer) {
					writer.close();
				}
				if (null != writerStream) {
					writerStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeTxt(Collection<String> list, String fileName, String encode) {
		writeTxt(list, fileName, Charset.forName(encode), false);
	}

	public static void writeTxt(Collection<String> list, String fileName, Charset charset) {
		writeTxt(list, fileName, charset, false);
	}

	public static void writeTxt(String fileName, String encode, String... list) {
		BufferedWriter writer = null;
		OutputStreamWriter writerStream = null;
		try {
			writerStream = new OutputStreamWriter(new FileOutputStream(fileName), encode);
			writer = new BufferedWriter(writerStream);
			for (String s : list) {
				writer.append(s);
				writer.newLine();
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != writer) {
					writer.close();
				}
				if (null != writerStream) {
					writerStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeTxt(Collection<String> list, String fileName, String encode, boolean additional) {
		writeTxt(list, fileName, Charset.forName(encode), additional);
	}

	public static void writeTxt(Collection<String> list, String fileName, Charset charset, boolean additional) {
		BufferedWriter writer = null;
		OutputStreamWriter writerStream = null;
		try {
			writerStream = new OutputStreamWriter(new FileOutputStream(fileName, additional), charset);
			writer = new BufferedWriter(writerStream);
			for (String s : list) {
				writer.append(s);
				writer.newLine();
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != writer) {
					writer.close();
				}
				if (null != writerStream) {
					writerStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeTxtLastLine(Collection<String> list, String fileName, String encode, boolean additional) {
		writeTxtLastLine(list, fileName, Charset.forName(encode), additional);
	}

	public static void writeTxtLastLine(Collection<String> list, String fileName, Charset charset, boolean additional) {
		BufferedWriter writer = null;
		OutputStreamWriter writerStream = null;
		try {
			writerStream = new OutputStreamWriter(new FileOutputStream(fileName, additional), charset);
			writer = new BufferedWriter(writerStream);
			int n = list.size();
			int i = 1;
			for (String s : list) {
				writer.append(s);
				if (i != n) {
					writer.newLine();
				}
				i++;
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != writer) {
					writer.close();
				}
				if (null != writerStream) {
					writerStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeTxt(String fileName, String encode, boolean additional, String... list) {
		BufferedWriter writer = null;
		OutputStreamWriter writerStream = null;
		try {
			writerStream = new OutputStreamWriter(new FileOutputStream(fileName, additional), encode);
			writer = new BufferedWriter(writerStream);
			for (String s : list) {
				writer.append(s);
				writer.newLine();
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != writer) {
					writer.close();
				}
				if (null != writerStream) {
					writerStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	public static synchronized void write(List<String> wlist) {
		NewTxtUtil.writeTxt(wlist, "F:/aaaaa.txt", "utf-8");
	}

	public static void reWriteTxt(String filePath, String sourceEncode, String targetEncode) {
		AtomicBoolean isfirst = new AtomicBoolean(true);
		readTxtFile(filePath, 100, sourceEncode, list -> {
			System.out.println("重写：" + filePath);
			if (isfirst.get()) {
				NewTxtUtil.writeTxt(list, filePath, targetEncode, false);
				isfirst.set(false);
			} else {
				NewTxtUtil.writeTxt(list, filePath, targetEncode, true);
			}
		});
	}

	public static void reWrite(String filePath, String sourceEncode, String targetEncode) {
		File file = new File(filePath);
		if (file.isDirectory()) {
			for (String path : file.list()) {
				reWrite(file.getPath() + File.separator + path, sourceEncode, targetEncode);
			}
		} else {
			if (file.getName().toLowerCase().endsWith("java")) {
				reWriteTxt(file.getPath(), sourceEncode, targetEncode);
			}
		}
	}

}
